<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>登录界面</title>
    <script src="/static/layui/layui.js"></script>
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">

</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
    <legend>用户登录</legend>
</fieldset>
<form class="layui-form" action="" method="post">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="username" lay-verify="required" autocomplete="off" class="layui-input"
                       value="admin">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password" lay-verify="required" autocomplete="off" class="layui-input"
                       value="123456">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="login">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>
    var $, layer, form;
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        form = layui.form;
        layer = layui.layer;
        //监听提交
        form.on('submit(login)', function (data) {
            // console.log(data);
            var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2{time: 10*1000}
            $.ajax({ //异步请求返回给后台
                url: "{:url(\"/admin/user/login\")}",
                type: 'POST',
                data: data.field,
                dataType: 'json',
                success: function (res) {
                    console.log(res);
                    layer.close(index);
                    // layer.msg(res.msg + "三秒后跳转..");
                    if (res.code == 200) {
                        localStorage.setItem("token", res.data['token']);//存储token
                        layer.open({
                            type: 1,//1单页面
                            offset: 'auto',
                            title: '生成的token',
                            content:
                                "<div ><textarea style='width: 580px;height:170px;' placeholder='请输入内容'>" + res.data['token'] + "</textarea> </div>",
                            area: ['600px', '200px'],
                            btn: ['查询用户列表', '查看本地Token', '注销Token'],
                            btnAlign: 'c',
                            yes: function () {
                                layer.open({
                                    type: 2,//2跳转到页面
                                    offset: 'auto',
                                    title: '用户列表',
                                    content: "{:url('/admin/user/userListPage')}",
                                    area: ['800px', '600px'],
                                });
                            }
                            , btn2: function (index, layero) {  //查看本地Token
                                layer.msg("当前Token为: " + localStorage.getItem("token"));
                                return false;
                            }
                            , btn3: function (index, layero) { //注销Token 后端注销,本地继续保留
                                $.ajax({
                                    url: "{:url('/admin/user/logout')}",
                                    type: 'POST',
                                    data: {},
                                    dataType: 'json',
                                    headers: {'Authorization': localStorage.getItem("token")},  //token
                                    success: function (e) {
                                        if (e.code == 200) {
                                            layer.msg("Token注销成功---" + e.msg);
                                            //后续返回登录页面
                                        }
                                    },
                                    error: function (e) {
                                        layer.msg("注销失败");
                                    }
                                });
                                return false;
                            }
                        });
                    } else {
                        layer.msg(res.msg);
                    }
                },
                error: function (e) {
                    layer.close(index);
                    layer.msg("登录失败");
                }
            });
            return false;
        });
    });
</script>
<!-- 底部结束 -->
</body>
</html>